package util;

import domain.Snake;

import java.util.LinkedList;
import java.util.Random;

public class Node {
    private Snake snake;
    private int x;
    private int y;

    public Node() {
    }

    public Node(int x, int y) {
        this.x = x;
        this.y = y;
    }



    public void random(Snake snake) {

        LinkedList<Node> body = snake.getBody();
        Random r = new Random();
        int i;
        while (true) {
            int newX = r.nextInt(50);
            int newY = r.nextInt(30);
            for (i = 0; i < body.size(); i++) {
                Node node = body.get(i);
                if (node.x == newX && node.y == newY) {
                    break;
                }
            }
            if(i==body.size()){
                this.x=newX;
                this.y=newY;
                this.snake=snake;
                break;
            }
        }
    }
        public void random() {
            snake=new Snake();
            LinkedList<Node> body = snake.getBody();
            Random r = new Random();
            int i;
            while (true) {
                int newX = r.nextInt(50);
                int newY = r.nextInt(30);
                for (i = 0; i < body.size(); i++) {
                    Node node = body.get(i);
                    if (node.x == newX && node.y == newY) {
                        break;
                    }
                }
                if(i==body.size()){
                    this.x=newX;
                    this.y=newY;
                    break;
                }
            }


    }

    /**
     * 获取
     *
     * @return x
     */
    public int getX() {
        return x;
    }

    /**
     * 设置
     *
     * @param x
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * 获取
     *
     * @return y
     */
    public int getY() {
        return y;
    }

    /**
     * 设置
     *
     * @param y
     */
    public void setY(int y) {
        this.y = y;
    }

    public String toString() {
        return "Node{x = " + x + ", y = " + y + "}";
    }




}
